#!/usr/bin/env Python
#-*-coding:utf-8-*-
from pyquery import PyQuery as pq
from .api import Emotion
from .until import Untiles
import re
import json

class Dispose_info(object):

    @classmethod
    def info_return(cls, string,x1):
        if 'href' in string:
            if x1 == '百度PC':  # 百度PC
                basic_url = 'https://www.baidu.com'
            elif x1 == '百度移动':
                basic_url = 'https://m.baidu.com'
            elif x1 == '360PC':
                basic_url = 'https://www.so.com'
            elif x1 == '360移动':
                basic_url = 'https://m.so.com'
            elif x1 == '搜狗PC':
                basic_url = 'https://www.sogou.com/web'
            elif x1 == '搜狗移动':
                basic_url = 'https://m.sogou.com'
            elif x1 == '神马':
                basic_url = 'https://m.sm.cn/s?q={}&from=smor&safe=1&by=tuijian&by2=general_entity'
            elif x1 == '搜狗PC新闻':
                basic_url = 'http://news.sogou.com'
            elif x1 == '百度PC新闻':
                basic_url = 'http://news.baidu.com'
            elif x1 == '搜狗移动新闻':
                basic_url = 'https://m.so.com'
            _string = []
            # 百度视频
            # print(string)
            string = re.sub(re.compile('<style.*?>.*?</style>|<script,*?>.*?</script>', re.S), '', string)
            pq_response = pq(string)
            for x in pq_response('a').items():
                res = x.text()
                item = {}
                href = x.attr('href')
                if href not in str(_string):
                    link = x.attr('href')
                    if 'http' in link:
                        item['link'] = """{}""".format(x.attr('href'))
                    else:
                        if x1 == '神马':
                            res_list = res.split('\n')[0]
                            item['link'] = """{}""".format(basic_url.format(res_list) + link)
                            # print(item['link'])
                        else:
                            item['link'] = """{}""".format(basic_url + link)
                    if '查看更多' in x.text() or '更多知道' in x.text():
                        continue
                    if Untiles.check_china(x.text()):
                        item['title'] = Untiles.until_str(x.text())
                        num = Emotion().post(x.text(), title='内容')['type']
                        item['type'] = num
                        _string.append(item)
                        # 保存数据仓库

            string = str(_string)
            # ss = eval(string)
            if "'type': -1" in string:
                type = -1
            elif "'type': 1" in string:
                type = 1
            else:
                type = 0
            res = (type,string)
            return res

        else:
            return string

    @classmethod
    def tittle_return(cls,string):
        _res = eval(string+']}')
        res = []
        title = _res.get('name')
        elements = _res.get('elements')
        if elements:
            for x in elements:
                item = {}
                item['title'] = x.get('name')
                item['link'] = x.get('wapurl')
                num = Emotion().post(x.get('name'), title='内容')
                item['type'] = num
                res.append(item)

            string = str(res)
            # ss = eval(string)
            if "'type': -1" in string:
                type = -1
            elif "'type': 1" in string:
                type = 1
            else:
                type = 0
            res = (title,(type, string))
            return res